const app = getApp()
const api = require("../../request/api")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: 0,
    packageList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initData()
  },

  async initData() {
    let result = await api.getAllPackage()
    console.log(result)
    this.setData({
      packageList: result.data
    })
  },
  // 扫码取件
  getPackage() {
    wx.scanCode({
      success: async (res) => {
        console.log(res.result)
        // 调用扫码取件接口
        let result = this.getQueryParams(res.result)
        console.log(result)
        let result2 = await api.finishPackage(result.id)
        if (result2.code == 400) {
          wx.showToast({
            title: '取件失败',
          })
        }
        wx.showToast({
          title: '取件成功',
        })
        // 触发刷新
        this.initData()
      }
    })
  },
  getQueryParams(url) {
    const regex = /[?&]([^=#]+)=([^&#]*)/g;
    const urlParams = {};
    let match;

    while ((match = regex.exec(url))) {
      urlParams[decodeURIComponent(match[1])] = decodeURIComponent(match[2]);
    }

    return urlParams;
  },

  // toSend
  toSend() {
    wx.navigateTo({
      url: '/pages/send/index',
    })
  }

})